package com.thoughtworks.biblioteca;

import java.util.ArrayList;
import java.util.*;

/**
 * Created by revathyr on 7/8/14.
 */
 class Library {
    //Book b;
    static List<Book> listOfBooks;

    static {
        listOfBooks = new ArrayList<Book>();
        //listOfBooks.add();
        listOfBooks.add(new Book("Harry Potter", "J.K.rowling", 1999, true));
        listOfBooks.add(new Book("The windmills of god", "Sidney sheldon", 2000, true));
        listOfBooks.add(new Book("If tomorrow comes", "Sidney sheldon", 2005, true));
    }
//Iterator bookIterator= Book.listOfBooks.iterator();
   // LinkedList book=new LinkedList();

    public boolean isAvailable(String ob) {

for(Book b:listOfBooks) {
    if (b.getBookName().equals(ob))
        return true;
}

        return false;
        }
public void listAllBooks() {
    System.out.format("%-35s%-35s%-35s%-35s", "BOOK", "AUTHOR", "PUBLISHED YEAR", "AVAILABILITY STATUS");
    for (Book b : listOfBooks) {

        System.out.println();
        if(b.isAvailable())
        System.out.format("%-35s%-35s%-35s%-35s", b.getBookName(), b.getAuthor(), b.getPublishedYear(), b.isAvailable());
    }
}
    public boolean checkOut(String bookName) {
        boolean available = isAvailable(bookName);
        if(available){
            //System.out.println("Inside 1 if");
int count =0;
            for(Book b:listOfBooks)
            {  if((b.getBookName().equals(bookName)) && b.isAvailable())

            {
            System.out.println("Thank you! Enjoy reading the book");
            listOfBooks.set(count,new Book(b.getBookName(),b.getAuthor(), b.getPublishedYear(),false));
             return true;
            }


                else
                count++;
        }}
            else
            System.out.println(bookName+ " is not available. Either choose another book or check the spelling");

    return false;
    }

   public boolean returnBook(String bookName){

       boolean available = isAvailable(bookName);
       int count=0;
       if(available)
       {

           for(Book b:listOfBooks)
           {  if(b.getBookName().equals(bookName) && (b.isAvailable()))

           {

               System.out.println("Not a valid book to return .Check if you are returning to the correct library");

           }

           else if(b.getBookName().equals(bookName)){

               listOfBooks.set(count,new Book(b.getBookName(),b.getAuthor(), b.getPublishedYear(),true));
               System.out.println("Thank you for returning the book ");
               return true;

           }
               else count++;

           }

       }
       else
           System.out.println("Not a valid book to return. Check if you are returning to the correct library");

       return false;

   }




}
  class LibraryTester
        {
        public static void main(String[] args) {


            Library ob = new Library();
            String bookName1;
            String bookName2, bookName3;
            Scanner input = new Scanner(System.in);
            int choice = 2;






                do {

                    System.out.println();
                    System.out.println("Choose what you wish to do..... 1.Browse books 2.Check out  3.Return Book 4.Quit");
                    choice = input.nextInt();
                    input.nextLine();

                    switch (choice) {
                        case 1:
                            ob.listAllBooks();
                            break;
                        case 2: {

                            System.out.println("enter the book Name to check out");
                            bookName1 = input.nextLine();

                             ob.checkOut(bookName1);
                            //System.out.println("returned bookName" + bookName3);
                        }
                        break;
                        case 3: {

                            System.out.println("enter the book name to return");
                            bookName1 = input.nextLine();
                             ob.returnBook(bookName1);
                        }
                        break;
                        case 4: {
                            System.out.println("Thank you for visiting our library ! ");
                            break;
                        }
                        default:
                            System.out.println("Select a valid option");

                    }


                } while (choice != 4);


            }

        }
